<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="时间" prop="voiceType">
        <el-date-picker
          v-model="dateRange"
          style="width: 100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="呼叫状态">
        <el-select
          v-model="formData.callStatus"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in callStatusList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目">
        <el-select
          v-model="formData.productId"
          filterable
          placeholder="请选择"
          clearable
          @change="onChangeProduct"
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务" prop="chatText">
        <el-select
          v-model="formData.taskId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in taskList"
            :key="item.id"
            :label="item.taskName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input
          v-model="formData.customerName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="chatText">
        <el-input
          v-model="formData.customerTel"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="坐席工号" prop="chatText">
        <el-input
          v-model="formData.workNumber"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="handleExport"
          v-hasPermi="['customerService:ttsTestList:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="onDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          size="mini"
          @click="handleExport"
          v-hasPermi="['customerService:ttsTestList:export']"
          >指定坐席</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <el-table-column label="客户号码" align="center" prop="customerTel" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="车系" align="center" prop="series" />
      <el-table-column
        label="省份"
        align="center"
        prop="province"
      ></el-table-column>
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="具体区县" align="center" prop="district" />
      <el-table-column label="上牌时间" align="center" prop="onBoardTime" />
      <el-table-column label="公里数" align="center" prop="kilometers" />
      <el-table-column
        label="是否可以正常过户"
        align="center"
        prop="isOwnership"
      />
      <el-table-column
        label="车辆来源"
        align="center"
        prop="carSource"
      ></el-table-column>
      <el-table-column
        label="预约时间（看车时间）"
        align="center"
        prop="appointmentTime"
      />
      <el-table-column label="坐席工号" align="center" prop="workNumber" />
      <el-table-column
        label="录入日期"
        align="center"
        prop="createTime"
      ></el-table-column>
      <el-table-column
        label="呼叫状态"
        align="center"
        prop="callStatus"
        :formatter="
          ({ callStatus }) => {
            return callStatus == 0
              ? '已呼'
              : callStatus == 1
              ? '未呼'
              : callStatus;
          }
        "
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { oldCarList, oldCarDelete } from "@/api/dataManagement/index";
import { getList } from "@/api/customerService/tts-test-list";
import { projectList } from "@/api/projectManagement/list";
import { taskList } from "@/api/taskManagement/index";
export default {
  name: "oldCar",
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        callStatus: "", //呼叫状态
        projectId: "",
        taskId: "",
        customerName: "",
        customerTel: "",
        workNumber: "",
      },
      projectId: "",
      callStatusList: [
        { dictValue: "0", dictLabel: "已呼" },
        { dictValue: "1", dictLabel: "未呼" },
      ], //呼叫状态
      productList: [],
      taskList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      dateRange: [], //日期范围
      total: 0,
      importShow: false, //导入
      addDialogShow: false,
      voiceTypeList: [],
      ids: false,
      multiple: true,
    };
  },
  watch: {
    "formData.productId": {
      handler(val) {
        if (val) {
          this.projectId = val;
          this.getTaskList();
        } else {
          this.projectId = "";
        }
      },
      immediate: true,
    },
  },
  async created() {
    this.getTaskList();
    this.getProductList();
    await this.getList();
  },
  methods: {
    // 任务列表
    async getTaskList() {
      const params = {
        projectId: this.projectId,
        taskName: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await taskList(params);
      if (code === 200) {
        this.taskList = rows;
      }
    },
    // 项目列表
    async getProductList() {
      const params = {
        status: "",
        projectName: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await projectList(params);
      if (code === 200) {
        this.productList = rows;
      }
    },
    onChangeProduct(event) {
      this.projectId = event;
    },
    // 列表查询
    async getList() {
      let params = {
        ...this.formData,
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await oldCarList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        callStatus: "", //呼叫状态
        projectId: "",
        taskId: "",
        customerName: "",
        customerTel: "",
        workNumber: "",
      };
      this.dateRange = [];
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const url = `/system/userdcar/export`;
      const formData = JSON.parse(JSON.stringify(this.formData));
      let data = {
        ...formData,
        ...this.pagination,
      };
      if (this.dateRange?.length) {
        data.params =
          typeof data.params === "object" &&
          data.params !== null &&
          !Array.isArray(data.params)
            ? data.params
            : {};
        data.params["beginTime"] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `newCar_${new Date().getTime()}.xlsx`
      );
    },

    onDelete() {
      const infoIds = this.ids;
      this.$modal
        .confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？')
        .then(function () {
          return oldCarDelete(infoIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
  },
};
</script>

<style lang="scss" scoped></style>
